package org.jivesoftware.webchat.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.infoview.db.DbUtil;

/**
 * 
 */
public class MsgServlet extends HttpServlet implements Servlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MsgServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		final String workgroup = request.getParameter("workgroup");
		final String chatID = request.getParameter("chatID");
//		final String sessionID = request.getParameter("sessionID");
		
//		final String username = request.getParameter("username");
		final String phone = request.getParameter("phone");
		final String email = request.getParameter("email");
		final String title = request.getParameter("title");
		final String content = request.getParameter("content");

		try {
			Connection conn = DbUtil.getConnection();
			
			String sql = "insert into ofmessage (content, phone, email, title) values (?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, content);
			pstmt.setString(2, phone);
			pstmt.setString(3, email);
			pstmt.setString(4, title);

			pstmt.execute();

			pstmt.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect("transcriptmain.jsp?workgroup=" + workgroup
				+ "&chatID=" + chatID);
	}

	public void init() throws ServletException {
		super.init();
	}
}